package com.acane.instagram.mapper;

import com.acane.instagram.pojo.Post;
import com.acane.instagram.pojo.Tag;
import com.acane.instagram.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper extends BaseMapper<Post> {
    @Select("SELECT id, user_id, content, like_count, comment_count, created_time, share_range, show_range, thumbnail_url" +
            " FROM post WHERE user_id = #{userId} ORDER BY created_time DESC")
    List<Post> selectBasicPostsByUserId(@Param("userId") String userId);

    @Select("SELECT url from post_url where post_id = #{postId}")
    List<String> selectPostWithImages(@Param("postId") String postId);

    @Select("SELECT post_id FROM post_recommend WHERE user_id = #{userId}")
    List<String> getRecommendPostIds(@Param("userId") String userId);

    @Select("<script>" +
            "SELECT * FROM post WHERE id IN " +
            "<foreach item='id' index='index' collection='recommendPostIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Post> selectRecommendPostList(@Param("recommendPostIds") List<String> recommendPostIds);

    @Insert("INSERT INTO post (id, user_id, content, like_count, comment_count, created_time, share_range, show_range, thumbnail_url, status, is_deleted)" +
            " VALUES (#{id}, #{userId}, #{content}, #{likeCount}, #{commentCount}, #{createdTime}, #{shareRange}, #{showRange}, #{thumbnailUrl}, #{status}, #{isDeleted})")
    int addPost(Post post);

    @Insert("INSERT INTO post_url (id, post_id, url) VALUES (#{id}, #{postId}, #{url})")
    int addPostUrl(@Param("id") String id, @Param("postId") String postId,@Param("url") String url);

    @Select("SELECT u.* FROM user u JOIN post p ON u.id = p.user_id WHERE p.id = #{postId}")
    User getUserByPostId(String postId);

    @Select("SELECT * FROM post WHERE id = #{postId}")
    Post getPostById(@Param("postId") String postId);

    @Update("UPDATE post SET like_count = like_count + 1 WHERE id = #{postId}")
    int likePost(@Param("postId") String postId);

    @Update("UPDATE post SET like_count = like_count - 1 WHERE id = #{postId}")
    int unlikePost(@Param("postId") String postId);

    @Update("UPDATE post SET comment_count = comment_count + 1 WHERE id = #{postId}")
    int addCommentCount(String postId);

    @Select("SELECT * FROM post WHERE content LIKE CONCAT('%', #{keyword}, '%')")
    List<Post> searchPost(String keyword);

    @Select("SELECT t.* FROM tags t JOIN post_tags pt ON t.id = pt.tag_id WHERE pt.post_id = #{postId}")
    List<Tag> getTagsByPostId(String postId);

    @Select("SELECT id, user_id, content, like_count, comment_count, created_time, share_range, show_range, thumbnail_url" +
            " FROM post ORDER BY created_time DESC")
    List<Post> getPostList();

    @Select("<script>" +
            "SELECT " +
            "  p.id, " +
            "  p.user_id AS userId, " +
            "  p.content, " +
            "  p.like_count AS likeCount, " +
            "  p.comment_count AS commentCount, " +
            "  p.created_time AS createdTime, " +
            "  p.share_range AS shareRange, " +
            "  p.show_range AS showRange, " +
            "  p.thumbnail_url AS thumbnailUrl, " +
            "  u.user_name AS 'user.userName', " +  // 修正字段名
            "  u.nick_name AS 'user.nickName', " +   // 修正字段名
            "  u.avatar AS 'user.avatar' " +
            "FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "<where>" +
            "  <if test='userName != null and userName != \"\"'>" +
            "    AND u.user_name LIKE CONCAT('%', #{userName}, '%')" + // 使用user_name字段
            "  </if>" +
            "  <if test='nickName != null and nickName != \"\"'>" +
            "    AND u.nick_name LIKE CONCAT('%', #{nickName}, '%')" + // 使用nick_name字段
            "  </if>" +
            "  <if test='content != null and content != \"\"'>" +
            "    AND p.content LIKE CONCAT('%', #{content}, '%')" +
            "  </if>" +
            "  <if test='startTime != null'>" +
            "    AND p.created_time &gt;= #{startTime}" +
            "  </if>" +
            "  <if test='endTime != null'>" +
            "    AND p.created_time &lt;= #{endTime}" +
            "  </if>" +
            "</where>" +
            "ORDER BY p.created_time DESC " +
            "LIMIT #{offset}, #{size}" +
            "</script>")
    List<Post> searchPosts(
            @Param("userName") String userName,
            @Param("nickName") String nickName,
            @Param("content") String content,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("offset") int offset,
            @Param("size") int size
    );

    // 修改后的基础分页查询
    @Select("SELECT " +
            "  id, " +
            "  user_id AS userId, " +
            "  content, " +
            "  like_count AS likeCount, " +
            "  comment_count AS commentCount, " +
            "  created_time AS createdTime, " +
            "  share_range AS shareRange, " +
            "  show_range AS showRange, " +
            "  thumbnail_url AS thumbnailUrl " +
            "FROM post " +
            "ORDER BY created_time DESC " +
            "LIMIT #{pageNum}, #{pageSize}")
    List<Post> getPostListByPage(int pageNum, int pageSize);

    @Update("UPDATE post SET status = 1 WHERE id = #{postId}")
    int passAction(String postId);

    @Update("UPDATE post SET status = 2 WHERE id = #{postId}")
    int failAction(String postId);
}
